package com.dys.filter;

import javax.servlet.*;
import java.io.IOException;
import java.util.Enumeration;

/**
 * @className: com.dys.filter.Filter01
 * @description: 过滤器
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/09/24 22:19
 */
public class Filter01 implements Filter {
    public Filter01() {
        System.out.println("Filter01的构造器......");
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("自定义过滤init()初始化方法......");
        // 读取初始化参数
        // 返回定名称的初始化参数的值。如果不存在返回null.
        String name = filterConfig.getInitParameter("name");
        System.out.println("name: " + name);
        // 获取所有初始化参数
        // 返回过滤器的所有初始化参数的名字的枚举集合。
        Enumeration<String> initParameterNames = filterConfig.getInitParameterNames();
        while (initParameterNames.hasMoreElements()) {
            String initName = initParameterNames.nextElement();
            System.out.println(initName + "=" + filterConfig.getInitParameter(initName));
        }
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("执行Filter01的doFilter()方法......");
        System.out.println("Filter01 正在过滤请求");
        filterChain.doFilter(servletRequest, servletResponse);
        System.out.println("Filter01 正在过滤响应");
    }

    @Override
    public void destroy() {
        System.out.println("自定义过滤器destroy()销毁方法......");
    }
}
